<?php
/**
 * 功能: 平台管理
 * User: WD
 * Date: 2018/8/30 0030
 * Time: 10:12
 */

namespace app\admin\controller;


use app\base\controller\Power;
use think\Db;

class Plat extends Power{
    protected $beforeActionList = [
        'first',

    ];
    protected function first(){
        $result = $this->testing();
    }
    /**
     * 平台邀请设置
     */
    public function index(){
        $result = Db::name('plat')->find();
        $this->assign([
            'plat'=>$result
        ]);
        return view('');
    }
    /**
     * 生成邀请码
     */
    public function mark(){
        $id = intval(input('id'));
        $mark = getRandomString(5).'p1';
        $data = [
            'mark'=>$mark
        ];
        if($id==0){
            $result = Db::name('plat')->insert($data);
        }else{
            $result = Db::name('plat')->where('id',$id)->update([ 'mark'=>$mark,'code_img'=>'']);
        }
        if($result){
            return json(['msg'=>'邀请码设置成功','ok'=>true]);
        }else{
            return json(['msg'=>'邀请码设置失败','ok'=>false]);
        }
    }

    /**
     * 修改邀请码
     */
    public function getMark(){
        $id = intval(input('id'));
        $mark = trim(input('mark'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }
        if(empty($mark)){
            return json(['msg'=>'邀请码不能为空','ok'=>false]);
        }
        $check = Db::name('user')->where('mark',$mark)->find();
        $plat  = Db::name('plat')->whereNotIn('id',$id)->where('mark',$mark)->find();
        if($check ||$plat){
            return json(['msg'=>'该邀请码已经存在','ok'=>false]);
        }else{
            $res = Db::name('plat')->where('id',$id)->update(['mark'=>$mark,'code_img'=>'']);
            if($res){
                return json(['msg'=>'修改成功','ok'=>true,'mark'=>$mark]);
            }else{
                return json(['msg'=>'修改失败','ok'=>false]);
            }
        }
    }

    /**
     * 平台名称设置
     */
    public function setName(){
        $id = intval(input('id'));
        $name = trim(input('name'));
        $data = [
            'name'=>$name
        ];
        if($id==0){
            $result = Db::name('plat')->insert($data);
        }else{
            $result = Db::name('plat')->where('id',$id)->update($data);
        }
        if($result){
            return json(['msg'=>'名称成功','ok'=>true]);
        }else{
            return json(['msg'=>'名称失败','ok'=>false]);
        }
    }
    /**
     * 生成二维码
     */
    public function create(){
        $id   = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }
        $mark = trim(input('mark'));
        $code = json_decode(scerweima2($mark),true);

        if($code['ok']){
            $result = Db::name('plat')->where('id',$id)->update(['code_img'=>$code['msg']]);
            if($result){
                return json(['msg'=>'生成二维码成功','ok'=>true]);
            }else{
                return json(['msg'=>'生成二维码失败','ok'=>false]);
            }
        }else{
            return json(['msg'=>$code['msg'],'ok'=>false]);
        }
    }

}